<template>
  <div class="app-container">
    <!--        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
              <el-form-item label="固体废物名称" prop="soName">
                <el-input
                  v-model="queryParams.soName"
                  placeholder="请输入固体废物名称"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="处置方式" prop="soHandleType">
                <el-select v-model="queryParams.soHandleType" placeholder="请选择处置方式" clearable size="small">
                  <el-option label="请选择字典生成" value="" />
                </el-select>
              </el-form-item>
              <el-form-item label="处置场地名称" prop="soHandleName">
                <el-input
                  v-model="queryParams.soHandleName"
                  placeholder="请输入处置场地名称"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="污染源编码" prop="soPollutionCode">
                <el-input
                  v-model="queryParams.soPollutionCode"
                  placeholder="请输入污染源编码"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="环境监察记录" prop="soRecord">
                <el-input
                  v-model="queryParams.soRecord"
                  placeholder="请输入环境监察记录"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="规范化排口(0,1)" prop="soHasNormalization">
                <el-input
                  v-model="queryParams.soHasNormalization"
                  placeholder="请输入规范化排口(0,1)"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['pollutant:solid:add']"
          icon="el-icon-plus"
          plain
          size="small"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['pollutant:solid:edit']"
          :disabled="single"
          icon="el-icon-edit"
          plain
          size="small"
          type="success"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['pollutant:solid:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="small"
          type="danger"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['pollutant:solid:export']"
          :loading="exportLoading"
          icon="el-icon-download"
          plain
          size="small"
          type="warning"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="solidList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <!--      <el-table-column align="center" label="固体废物排放ID" prop="soId"/>-->
      <el-table-column align="center" label="固体废物名称" prop="soName"/>
      <el-table-column align="center" label="处置方式" prop="soHandleType"/>
      <el-table-column align="center" label="处置场地名称" prop="soHandleName"/>
      <!--      <el-table-column align="center" label="污染源编码" prop="soPollutionCode"/>-->
      <el-table-column align="center" label="环境监察记录" prop="soRecord">
        <template slot-scope="scope" v-if="scope.row.soRecord">
          <a :href="`${baseUrl}${scope.row.soRecord}`" target="_blank"
             class="buttonText">{{ scope.row.soRecord.toString().substring(scope.row.soRecord.lastIndexOf("/") + 1) }}</a>
        </template>
      </el-table-column>
      <el-table-column align="center" label="规范化排口" prop="soHasNormalization"/>
      <!--      <el-table-column align="center" label="备注" prop="remark"/>-->
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['pollutant:solid:edit']"
            size="small"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            v-hasPermi="['pollutant:solid:remove']"
            size="small"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改固体废物排放对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="35%">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="固体废物名称" prop="soName">
          <el-input v-model="form.soName" placeholder="请输入固体废物名称"/>
        </el-form-item>
        <el-form-item label="处置方式" prop="soHandleType">
          <!--  垃圾填埋场、焚烧、尾矿库        -->
          <div style="width: 100%;">
            <el-select v-model="shtIndex" clearable placeholder="请选择" style="width: 100%;"
                       @change="shtChange">
              <el-option v-for="item in sht" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </div>
        </el-form-item>
        <el-form-item label="处置场地名称" prop="soHandleName">
          <el-input v-model="form.soHandleName" placeholder="请输入处置场地名称"/>
        </el-form-item>
        <el-form-item label="污染源编码" prop="soPollutionCode">
          <el-input v-model="form.soPollutionCode" placeholder="请输入污染源编码"/>
        </el-form-item>
        <el-form-item label="环境监察记录" prop="soRecord">
         <FileUpload v-model="form.soRecord"/>
        </el-form-item>
        <el-form-item label="规范化排口" prop="soHasNormalization">
          <div style="width: 100%;">
            <el-select v-model="pkIndex" clearable placeholder="请选择" style="width: 100%;"
                       @change="pkChange">
              <el-option v-for="item in pk" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </div>
        </el-form-item>
        <el-form-item label="处置场地照片" prop="remark">
          <ImageUpload v-model="form.remark"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer tianjie_button" style="text-align: center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addSolid, delSolid, exportSolid, getSolid, listSolid, updateSolid} from "@/api/pollutant/solid";
import {getToken} from "@/utils/auth";
import {InfoItem} from "@/api/enterprise/info";
import {isIntegerNotMust,validatePhoneTwo,validateIdNo,validateEMail} from "@/api/validate/validate";


export default {
  name: "Solid",
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      sht: [
        {
          value: '0',
          label: '焚烧'
        },
        {
          value: '1',
          label: '垃圾填埋场'
        },
        {
          value: '2',
          label: '尾矿库'
        }
      ],
      pk: [
        {
          value: '0',
          label: '是'
        },
        {
          value: '1',
          label: '否'
        }
      ],
      pkIndex: '1',
      shtIndex: '0',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 固体废物排放表格数据
      solidList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        soName: null,
        soHandleType: null,
        soHandleName: null,
        soPollutionCode: null,
        soRecord: null,
        soHasNormalization: null,
        epId:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        soName: [
          {required: true, message: "固体废物名称不能为空", trigger: "blur"}
        ],
        soHandleType: [
          {required: true, message: "处置方式不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    shtChange(e) {
      this.form.soHandleType = e;
    },
    pkChange(e) {
      this.form.soHasNormalization = e;
    },
    /** 查询固体废物排放列表 */
    getList() {
      this.loading = true;
      const username = this.$store.state.user.name;
      InfoItem(username).then(res => {
        if (res.hasOwnProperty("data")) {
          this.queryParams.epId = res.data.epId;
          listSolid(this.queryParams).then(response => {
            this.solidList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        soId: null,
        soName: null,
        soHandleType: null,
        soHandleName: null,
        soPollutionCode: null,
        soRecord: null,
        soHasNormalization: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        epId:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.soId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加固体废物排放";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const soId = row.soId || this.ids
      getSolid(soId).then(response =>{
        this.form = response.data;
        this.open = true;
        this.title = "修改固体废物排放";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.soId != null) {
            updateSolid(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const username = this.$store.state.user.name;
            InfoItem(username).then(res => {
              if (res.hasOwnProperty("data")) {
                this.form.epId = res.data.epId;
              }
              addSolid(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const soIds = row.soId || this.ids;
      this.$confirm('是否确认删除固体废物排放编号为"' + soIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delSolid(soIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有固体废物排放数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportSolid(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>
